-- 引入必要的命名空间
local GameObject = UnityEngine.GameObject
local EventTrigger = UnityEngine.EventSystems.EventTrigger
local EventTriggerType = UnityEngine.EventSystems.EventTriggerType
local RectTransformUtility = UnityEngine.RectTransformUtility
local RectTransform = UnityEngine.RectTransform
local Vector2 = UnityEngine.Vector2
local Vector3 = UnityEngine.Vector2
Screen = UnityEngine.Screen
local Mathf = UnityEngine.Mathf

YaoGan = {}

-- 1. 获取名为 "Mobile Joystick Zone" 的游戏对象
local joystickZone = GameObject.Find("Mobile Joystick Zone")
if joystickZone == nil then
    print("未找到名为 Mobile Joystick Zone 的游戏对象")
    return
end

local mobilejoystick = joystickZone.transform:Find("Mobile Joystick")
if mobilejoystick == nil then
    print("未找到Panel")
    return
end

local joystickOutline = mobilejoystick.transform:Find("Joystick Outline")
if joystickOutline == nil then
    print("未找到joystickOutline")
    return
end

local joystickKnob = joystickOutline.transform:Find("Joystick Knob")
if joystickKnob == nil then
    print("未找到joystickKnob")
    return
end

local canControl = false
local joystickPos = Vector3.zero
local move = Vector3.zero
local moveFactor = 1200

-- 2. 获取 EventTrigger 组件
local eventTrigger = joystickZone:GetComponent(typeof(EventTrigger))
if eventTrigger == nil then
    -- 如果没有 EventTrigger 组件，则添加一个
    eventTrigger = joystickZone:AddComponent(typeof(EventTrigger))
end

function YaoGan:GetMoveVector()
    return move
end

local function ShowJoystick()

    joystickOutline.gameObject:SetActive(true)
    canControl = true --开始控制Joystick
end

local function HideJoystick()

    joystickOutline.gameObject:SetActive(false);
    canControl = false; --停止控制Joystick

    move = Vector3.zero;
end

-- 3. 定义回调函数
local function OnPointerDown()
    print("Mobile Joystick Zone 被点击了！")
    -- 在这里添加你的逻辑
    --获取鼠标点击的位置，设置给joystickOutline的位置

    joystickPos = Input.mousePosition
    local rectTransform = joystickOutline:GetComponent(typeof(RectTransform))
    rectTransform.position = joystickPos

    ShowJoystick()
end


local function ControlJoystick() 
    local currentMousePos = Input.mousePosition
    local direction = currentMousePos - joystickPos
    
    --Joystick的移动范围限制
    local moveMagnitude = direction.magnitude * moveFactor / Screen.width
    
    moveMagnitude = Mathf.Min(moveMagnitude, joystickOutline.rect.width / 2) --Joystick的最大移动距离限制

    move = direction.normalized * moveMagnitude --Joystick的移动方向

    local targetPos = joystickPos + move --Joystick的目标位置

    joystickKnob.position = targetPos

    if(Input:GetMouseButtonUp(0)) then
        HideJoystick()
    end
    
end


-- 4. 创建 EventTrigger.Entry 并设置事件类型
local entry = EventTrigger.Entry()
entry.eventID = EventTriggerType.PointerDown

-- 5. 将回调函数绑定到 EventTrigger.Entry
entry.callback = EventTrigger.TriggerEvent()
entry.callback:AddListener(OnPointerDown)

-- 6. 将 EventTrigger.Entry 添加到 EventTrigger 的 triggers 列表中
eventTrigger.triggers:Add(entry)


function Update2()

    --print("Update2")

    if canControl == true then
        ControlJoystick()
    end

    PlayerMove:ManageMovement()

end